<?php
use Illuminate\Console\Command;
use Carbon\Carbon;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Input\InputOption;

class Upgrade extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'upgrade';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'upgrade';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire() {
    	//2.升级产品的图片
    	$mode = $this->argument("mode");
    	if ($mode == 0){
    		$this->upgradeBanner();
    	} else if ($mode==1){
    		$this->upgradeProductImages();
    	} else {
    		$this->upgradeProduct();
    	}
    	//$option = $this->option("option");
    	//Log::info('mode='.$mode.",option=".$option);
    }
    
    private function upgradeBanner(){
    	//1.升级首页图片，
    	$banners = Banner::all();
    	$c = $this;
    	$banners->each(function($banner) use ($c){
    		//     		md5($originName.str_random(8))
    		if (!empty($banner->image) && !starts_with($banner->image,Config::get('mao.oss.base')) ){
    			$key = $c->upload($banner->image);
    			$banner->update(array("image"=>$key));
    		}
    	});
    }
    
    private function upgradeProductImages(){
    	$images = ProductImages::all();
    	$c = $this;
    	$images->each(function($image) use ($c){
    		//     		md5($originName.str_random(8))
    		if (!empty($image->image) && !starts_with($image->image,Config::get('mao.oss.base')) ){
	    		$key = $c->upload($image->image);
	    		$image->update(array("image"=>$key));
    		}
    	});
    }
    
    private function upgradeProduct(){
    	$c = $this;
    	//升级产品的默认图片
    	$products = Products::onlyTrashed()->get();
    	$products->each(function($p) use ($c) {
    		Log::info($p->image);
    		if (!empty($p->image) && !starts_with($p->image,Config::get('mao.oss.base')) ){
    			$key = $c->upload($p->image);
    			$p->update(array("image"=>$key));
    			Log::info($p->id."upgrade success,key=".$key);
    		}
    	});
    }
    
    private function upload($path){
    	$base = Config::get("mao.image_base");
    	$filePath = $base.$path;
    	$key = md5($path.str_random(8));
    	$key = OSS::put($key, $filePath);
    	return $key;
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments() {
    	//name,mode,desc,default
        return array(
        	array('mode', InputArgument::OPTIONAL, '控制升级的模式',0),
    	);
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions() {
    	/*
    	* @param string $name        The option name
    	* @param string $shortcut    The shortcut (can be null)
    	* @param int    $mode        The option mode: One of the InputOption::VALUE_* constants
    	* @param string $description A description text
    	* @param mixed  $default     The default value (must be null for InputOption::VALUE_REQUIRED or InputOption::VALUE_NONE)
    	*/
    	return array(
    			array('option', "o", InputOption::VALUE_OPTIONAL, 'An example option.', 0),
    	);
    }

}
